import 'package:flutter/material.dart';
import 'package:necproject/pages/home/home_drawer.dart';
import 'package:necproject/utils/g.dart';

import 'home/home.dart';
import 'category/category.dart';
import 'news/news.dart';
import 'mine/mine.dart';

class IndexPage extends StatefulWidget {
  int currentIndex = 0;
  IndexPage({Key? key, int this.currentIndex = 0}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  final List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(
      backgroundColor: Colors.blue,
      icon: Icon(Icons.home),
      label: G.t('home'),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.green,
      icon: Icon(Icons.message),
      label: G.t('news'),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.amber,
      icon: Icon(Icons.shopping_cart),
      label: G.t('cart'),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      label: G.t('mine'),
    ),

    // BottomNavigationBarItem(
    //   backgroundColor: Colors.blue,
    //   icon: Icon(Icons.home),
    //   label: 'home',
    // ),
    // BottomNavigationBarItem(
    //   backgroundColor: Colors.green,
    //   icon: Icon(Icons.message),
    //   label: 'news',
    // ),
    // BottomNavigationBarItem(
    //   backgroundColor: Colors.amber,
    //   icon: Icon(Icons.shopping_cart),
    //   label: 'cart',
    // ),
    // BottomNavigationBarItem(
    //   backgroundColor: Colors.red,
    //   icon: Icon(Icons.person),
    //   label: 'mine',
    // ),
  ];

  final pages = [
    Home(),
    News(),
    Category(),
    Mine(),
  ];

  // int currentIndex = 0;
  final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    //注册一个回调函数yourCallback
    WidgetsBinding.instance?.addPostFrameCallback((_) => yourCallback(context));
    // WidgetsBinding.instance?.addPersistentFrameCallback((timeStamp) {});
  }

  @override
  Widget build(BuildContext context) {
    return OrientationBuilder(
      builder: (BuildContext context, Orientation orientation) {
        return Scaffold(
          key: _scaffoldkey,
          appBar: AppBar(
              title: Text(bottomNavItems[widget.currentIndex].label.toString()),
              leading:
                  MediaQuery.of(context).orientation == Orientation.landscape
                      ? Builder(builder: (context) {
                          return IconButton(
                            icon: Icon(Icons.menu),
                            onPressed: () {
                              _scaffoldkey.currentState?.openDrawer();
                            },
                          );
                        })
                      : null,
              centerTitle: true),
          bottomNavigationBar: BottomNavigationBar(
              items: bottomNavItems,
              currentIndex: widget.currentIndex,
              type: BottomNavigationBarType.fixed,
              // type: BottomNavigationBarType.shifting,
              onTap: (index) {
                _changePage(index);
              }),
          body: pages[widget.currentIndex],
          drawer: MediaQuery.of(context).orientation == Orientation.landscape
              ? null
              : MyDrawer(),
          // onDrawerChanged: (isOpen) {
          //   if (isOpen) {
          //     if (MediaQuery.of(context).orientation == Orientation.landscape) {
          //       if (!_scaffoldkey.currentState!.isDrawerOpen) {
          //         print('4444444444444');
          //         _scaffoldkey.currentState?.openDrawer();
          //       }
          //     }
          //   }
          // },
        );
      },
    );
  }

  void _changePage(int index) {
    if (index != widget.currentIndex) {
      setState(() {
        widget.currentIndex = index;
      });
    }
  }

  yourCallback(BuildContext context) {
    print('111111111111111');
    // if (MediaQuery.of(context).orientation == Orientation.portrait) {
    //   if (!_scaffoldkey.currentState!.isDrawerOpen) {
    //     setState(() {
    //       print('22222222222222222');
    //       _scaffoldkey.currentState?.openDrawer();
    //     });
    //   }
    // }
  }
}
